package com.woniu.inlet.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.woniu.adapter.WorkReportAdapter;
import com.woniu.command.Command;
import com.woniu.command.handcommand.AddHandCommand;
import com.woniu.command.handcommand.DelHandCommand;
import com.woniu.command.handcommand.UpdateHandCommand;
import com.woniu.command.handcommand.UrgentHandCommand;
import com.woniu.inlet.vo.ProcessHandoverVo;
import com.woniu.outlet.po.ProcessHandover;
import com.woniu.service.ProcessHandoverService;
import com.woniu.service.WorkReportService;
import com.woniu.util.JwtTokenUitl;
import com.woniu.util.ResponseResult;
import com.woniu.util.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@Api(tags = "工序交接controller")
@RequestMapping("ProcessHandover/")
public class ProcessHandoverController {
    @Autowired
    private ProcessHandoverService processHandoverService;
    @Autowired
    private WorkReportService workReportService;

    @ApiOperation("查询所有未报工的工序交接")
    @GetMapping("getProcessHandoverList")
    public ResponseResult getProcessHandoverList(String orderCode) throws JsonProcessingException {
        List<ProcessHandover> processHandoverList = processHandoverService.getProcessHandoverList(orderCode);
        List<ProcessHandoverVo> processHandoverVoList = new ArrayList<>();
        for (ProcessHandover processHandover : processHandoverList) {
            ProcessHandoverVo processHandoverVo = new ProcessHandoverVo();
            BeanUtils.copyProperties(processHandover,processHandoverVo);
            processHandoverVoList.add(processHandoverVo);
        }
        return new ResponseResult(processHandoverVoList,"查询所有未报工的工序交接",200);
    }

    @PostMapping("urgent")
    @ApiOperation("工序交接")
    @ApiOperationSupport(ignoreParameters = {"id","handoverPersonId","handoverName","handoverTime","behandoverPersonId","beHandoverName","spare1","spare2","spare3"})
    public ResponseResult urgent(@RequestBody ProcessHandover processHandover,HttpServletRequest request){
        String userName = JwtTokenUitl.getUserName(request.getHeader("tokenStr"));
        processHandover.setHandoverName(userName);
        Command urgentHandCommand = new UrgentHandCommand();
        urgentHandCommand.execute(processHandover);
        return new ResponseResult(200,"工序交接成功");
    }

    /** 配件入库
     * @param processHandover
     * @return
     */
    @PostMapping("addProcessHandover")
    @ApiOperation("成品入库")
    @ApiOperationSupport(ignoreParameters = {"id","handoverPersonId","handoverName","behandoverPersonId","handoverTime","beHandoverName","spare1","spare2","spare3"})
    public ResponseResult addProcessHandover(@RequestBody ProcessHandover processHandover, HttpServletRequest request){
        String userName = JwtTokenUitl.getUserName(request.getHeader("tokenStr"));
        processHandover.setHandoverName(userName);
        Command addHandCommand = new AddHandCommand();
        addHandCommand.execute(processHandover);
        return new ResponseResult(200,"成品入库成功");
    }

//    /**
//     * @param processHandover
//     * @return
//     */
//    @PutMapping("updateProcessHandover")
//    @ApiOperation("修改工序交接")
//    public ResponseEntity<String> updateProcessHandover(@RequestBody ProcessHandover processHandover){
//        Command updateHandCommand = new UpdateHandCommand();
//        updateHandCommand.execute(processHandover);
//        return new ResponseEntity<>("OK",HttpStatus.OK);
//    }

//    @DeleteMapping("delProcessHandover")
//    @ApiOperation("删除工序交接")
//    public ResponseResult delProcessHandover(Integer id){
//        Command delHandCommand = new DelHandCommand();
//        delHandCommand.execute(id);
//        return new ResponseResult(200,"成品入库成功");
//    }

    /** 可以根据订单编号查询已报工的列表
     * @param orderCode
     * @return
     * @throws JsonProcessingException
     */
    @GetMapping("getWorkReport")
    @ApiOperation("查询所有已报工的工序交接")
    public ResponseResult getWorkReport(String orderCode) throws JsonProcessingException {
        List<ProcessHandover> workReport = workReportService.getWorkReport(orderCode);
        List<ProcessHandoverVo> processHandoverVos = new ArrayList<>();
        for (ProcessHandover processHandover : workReport) {
            ProcessHandoverVo processHandoverVo = new ProcessHandoverVo();
            BeanUtils.copyProperties(processHandover,processHandoverVo);
            processHandoverVos.add(processHandoverVo);
        }
        return new ResponseResult(processHandoverVos,"成品入库成功",200);
    }

    @PutMapping("workReport")
    @ApiOperation("报工")
    public ResponseResult workReport(Integer id,Integer num,HttpServletRequest request){
        ProcessHandover processHandover = new ProcessHandover();
        processHandover.setId(id);
        processHandover.setSpare1("已报工");
        processHandover.setSpare2(TimeUtil.getNowTime(new Date()));
        processHandover.setSpare3(num+"");
        String token = request.getHeader("tokenStr");
        String userName = JwtTokenUitl.getUserName(token);
        processHandover.setBeHandoverName(userName);
        workReportService.workReport(processHandover);
        return new ResponseResult(200,"成品入库成功");
    }
}
